package top.someapp.applib.webview;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.util.Map;

public class WebViewExt extends WebView {

    private boolean useScrollBar;
    private boolean useJavascript = true;
    //    private boolean clearCache;
    //    private boolean clearCookies;
    //    private boolean hidden;
    private String userAgent = "Mozilla/5.0 (Linux; Android 6.0;) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36";
    private Map<String, String> headers;
    private boolean useZoom;
    private boolean displayZoomControls;
    private boolean useLocalStorage = true;
    private boolean useOverviewMode;
    private boolean supportMultipleWindows;
    private boolean appCacheEnabled;
    private boolean allowFileURLs = true;
    private boolean useWideViewPort;
    private String invalidUrlRegex;
    private boolean geolocationEnabled;
    private boolean debuggingEnabled;

    public WebViewExt(final Context context) {
        super(context);
        init();
    }

    public WebViewExt(Context context, AttributeSet attrs) { // for using in layout xml file.
        super(context, attrs);
        init();
    }

    public boolean isUseScrollBar() {
        return useScrollBar;
    }

    public void setUseScrollBar(boolean useScrollBar) {
        this.useScrollBar = useScrollBar;
    }

    private void init() {
//        setScrollContainer(useScrollBar);
        setVerticalScrollBarEnabled(useScrollBar);
        setHorizontalScrollBarEnabled(useScrollBar);

        WebSettings settings = this.getSettings();
        settings.setJavaScriptEnabled(useJavascript);
        settings.setBuiltInZoomControls(useZoom);
        settings.setSupportZoom(useZoom);
        settings.setDisplayZoomControls(displayZoomControls);
        settings.setDomStorageEnabled(useLocalStorage);
        settings.setLoadWithOverviewMode(useOverviewMode);
        settings.setJavaScriptCanOpenWindowsAutomatically(supportMultipleWindows);
        settings.setSupportMultipleWindows(supportMultipleWindows);
        settings.setAppCacheEnabled(appCacheEnabled);
        settings.setAllowFileAccessFromFileURLs(allowFileURLs);
        settings.setAllowUniversalAccessFromFileURLs(allowFileURLs);
        settings.setUseWideViewPort(useWideViewPort);

// Handle debugging
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setWebContentsDebuggingEnabled(debuggingEnabled);
        }

//        webViewClient.updateInvalidUrlRegex(invalidUrlRegex);
        settings.setGeolocationEnabled(geolocationEnabled);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        }

        if (userAgent != null) {
            settings.setUserAgentString(userAgent);
        }
    }

    @Override
    public void scrollTo(int x, int y) {
        if (!useScrollBar) {
            x = 0;
            y = 0;
        }
        super.scrollTo(x, y);
    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
        if (!useScrollBar) {
            scrollTo(0, 0);
        } else {
//            super.onScrollChanged(l, t, oldl, oldt);
        }
    }
}